<!DOCTYPE html>
<html>
<head>   
    <title>users</title>   
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>          
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">                    
                    <el-form-item label="UserName">
                        <el-input v-model="searchData.userName" placeholder="User name"></el-input>
                    </el-form-item>        
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Add</el-button>
                    </el-form-item>
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="userName" label="User Name" min-width="15%"></el-table-column>
                    <el-table-column prop="accessKey" label="AccessKey" min-width="40%"></el-table-column>                 
                    <el-table-column prop="statusName" label="Status" min-width="10%"></el-table-column>
                    <el-table-column label="Operation" min-width="30%">
                        <template slot-scope="scope">                                      
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                            <el-button @click="handlePwdEdit(scope.row)" type="text" size="small">修改密码</el-button>
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[5, 10, 15]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form :model="dialogForm"> 
                    <el-form-item label="User Name" :label-width="formLabelWidth" v-if="noUpdatePassword">
                        <el-input v-model="dialogForm.userName" autocomplete="off"></el-input>
                    </el-form-item>                   
                    <el-form-item label="AccessKey" :label-width="formLabelWidth" v-if="noUpdatePassword">
                        <el-input v-model="dialogForm.accessKey" autocomplete="off" type="textarea"></el-input>
                    </el-form-item>                  
                    <el-form-item label="Enable" :label-width="formLabelWidth" v-if="noUpdatePassword">
                        <el-select v-model="dialogForm.enable" placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="Password" :label-width="formLabelWidth" v-if="dialogForm.updatePwd">
                        <el-input placeholder="请输入密码" v-model="dialogForm.userPassword" autocomplete="off" maxlength=32 minlenght=8 show-password></el-input>
                    </el-form-item>                  
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
              </el-dialog>
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();
        function docLoaded() {           
            var queryApp = new Vue({el: '#queryForm', data: {                                
                    searchData:{ 
                        userName: "", 
                        pageNum:1,
                        pageSize:5
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:true,name:"normal"},{status:false,name:"disable"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{                      
                        id:0,
                        userName:"",
                        userPassword: "",
                        accessKey:"",                             
                        enable: true,
                        updatePwd: false
                    },
                    noUpdatePassword:false
                },
                methods: {  
                    onSubmit:function(event){
                        this.searchData.pageNum = 1;
                        this.loadTableData(event);
                    },                      
                    loadTableData:function(event){
                        httpPost(apiUserPage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.loadTableData();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.loadTableData();
                    },                  
                    handleEdit(row){
                        this.dialogForm = {
                            id:row.id, 
                            userName:row.userName,                            
                            accessKey:row.accessKey, 
                            userPassword: "",                               
                            enable:row.enable,
                            updatePwd:false
                        };                       
                        this.dialogTitle = "Update User";
                        this.dialogMode = 0;
                        this.noUpdatePassword = true;
                        this.editDialogFormVisible = true;
                    },                   
                    handleAdd(){
                        this.dialogForm = {
                            id:0,
                            userName:"",
                            accessKey:"",   
                            userPassword: "",                                                                         
                            enable:true,
                            updatePwd:true
                        };                           
                        this.dialogTitle = "Add New User";
                        this.dialogMode = 1;
                        this.noUpdatePassword = true;
                        this.editDialogFormVisible = true;
                    },
                    handlePwdEdit(row){
                        this.dialogForm = {
                            id:row.id, 
                            userName:row.userName,  
                            userPassword: "",   
                            updatePwd:true
                        };                       
                        this.dialogTitle = "Update Password";
                        this.dialogMode = 2;
                        this.noUpdatePassword = false;
                        this.editDialogFormVisible = true; 
                    },
                    editSubmit:function(event){
                        this.editDialogFormVisible = false; 
                        var apiRequest = null;
                        switch (this.dialogMode){
                            case 0:
                                apiRequest = apiUserUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));break;
                            case 1:
                                apiRequest = apiUserAdd(this,router.authInfo,JSON.stringify(this.dialogForm));break;                            
                            case 2:
                                apiRequest = apiUserPwdUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));break;
                        }                       
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);                         
                            context.loadTableData();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }                   
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        userName:item.userName,                        
                        accessKey:item.accessKey,                                                           
                        enable:item.enable,
                        statusName:(item.enable)?"normal":"disable"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }
    </script>
</body>
</html>
